package com.mikao.config;


import com.mikao.dto.vo.SysUserDto;
import com.mikao.entity.SysUser;
import com.mikao.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @author KennyDavid
 */

@Component
public class CustomerAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CdnProperties cdnProperties;

    public CustomerAuthenticationSuccessHandler(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        SysUserDto byAccount = sysUserService.findByAccount(authentication.getName());
        HttpSession session = request.getSession();
        session.setAttribute("sysUser", byAccount);
        session.setAttribute("currentUserId", byAccount.getId());
        response.sendRedirect("management");
    }
}
